package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {


    @Autowired // 注入
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMappe;

    @Autowired
    private DishMapper dishMapper;
    // 分页查询
    @Override
    public Page<Setmeal> findAll(Integer pageNum, Integer pageSize, String name) {
        //分页查询构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        // 先判断传过来的参数不为空再去模糊查询
        qw.like(StrUtil.isNotBlank(name),Setmeal::getName,name);
        // 开启分页  把条件加上去 开始页pageNum 和每页个数 pageSize
        Page<Setmeal> page = new Page<>(pageNum,pageSize);
        // 在调用mapper方法查询分页 第一个参数就是分页  第二个就是查询条件
        page = setmealMapper.selectPage(page, qw);
        // 遍历每个值
        for (Setmeal record : page.getRecords()) {
            // 根据套餐再去查询菜系的id
            Category category = categoryMapper.selectById(record.getCategoryId());
         // 封装对象
           record.setCategoryName(category.getName());
         // 再根据id查询封装菜品列表
            LambdaQueryWrapper<SetmealDish> we = new LambdaQueryWrapper<>();
            we.eq(SetmealDish::getSetmealId,record.getId());
            List<SetmealDish> setmealDishes = setmealDishMappe.selectList(we);
            record.setSetmealDishes(setmealDishes);
        }
        return page;
    }

    // 新增
    @Override
    public void save(Setmeal setmeal) {
       // 直接调用mapper的新增
        setmealMapper.insert(setmeal);
        // 遍历前端提交套餐列表
        List<SetmealDish> qw = setmeal.getSetmealDishes();
        for (SetmealDish dish : qw) {
            //关联套餐id
            dish.setSetmealId(setmeal.getId());
            // 保存中间表
            setmealDishMappe.insert(dish);
        }
    }
    // 删除
    @Override
    public void delete(List<Long> ids) {
       // 先构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        // 再去让他判断起售状态不能删除
        qw.in(Setmeal::getId,ids);
        qw.eq(Setmeal::getStatus,1); //1 就是起售状态
        Integer setmeals = setmealMapper.selectCount(qw);
        if (setmeals>0){
            throw new CustomException("套餐中有菜品在起售");
        }
        // 删除套餐
        setmealMapper.deleteBatchIds(ids);
        // 再去删除中间表的菜品
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishMappe.delete(setmealDishLambdaQueryWrapper);
    }
    // 修改回显
    @Override
    public Setmeal updateById(long id) {
        // 回显返回的应该是一个实体类
      // 先根据前端传过来的id 获得一个套餐对象
        Setmeal setmeal = setmealMapper.selectById(id);
        //构建条件 构建一个套餐菜品关系的实体类
        LambdaQueryWrapper<SetmealDish> st = new LambdaQueryWrapper<>();
        //对比套餐菜品关系的id与传过来的id
        st.eq(SetmealDish::getSetmealId,id);
      //   在用套餐菜品关系查询
        List<SetmealDish> dishes = setmealDishMappe.selectList(st);
        // 封装
        setmeal.setSetmealDishes(dishes);

        return setmeal;

    }

     //修改
    @Override
    public void  update(Setmeal setmeal) {
        // 先修改 除菜品以外的信息
        setmealMapper.updateById(setmeal);
        // 在构建条件
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMappe.delete(qw);

        List<SetmealDish> qwq = setmeal.getSetmealDishes();
        for (SetmealDish dish : qwq) {
            //关联套餐id
            dish.setSetmealId(setmeal.getId());
            // 保存中间表
            setmealDishMappe.insert(dish);
        }

    }
    // 停用启用
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);

        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmealMapper.update(setmeal,qw);


    }
    //查询所有套餐
    @Override
    public List<Setmeal> findSetmeal(Long categoryId, Integer status) {
        // 构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId); //对比id
        qw.eq(Setmeal::getStatus,status);  //对比状态码
        return setmealMapper.selectList(qw); //直接返回结果 因为不需要在遍历菜品了


    }
    // 点击图片 自动弹出套餐
    @Override
    public List<Dish> taocan(Long id) {
        //1. 从中间表获取菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishMappe.selectList(wrapper);

        //2. 创建一个集合用于封装返回菜品信息
        List<Dish> dishList = new ArrayList<>();

        //3. 遍历得到菜品列表
        for (SetmealDish setmealDish : setmealDishList) {
            //根据菜品id从菜品表查询菜品详情
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            dish.setCopies(setmealDish.getCopies());//设置份数
            dishList.add(dish);//添加到返回集合
        }

        //4. 返回
        return dishList;
    }
    }



